package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.MyBusinessException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.ApartmentInfoMapper;
import com.atguigu.lease.web.admin.mapper.LabelInfoMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
   @Autowired
   ApartmentInfoMapper apartmentInfoMapper;

   @Autowired
    LabelInfoService labelInfoService;
   @Autowired
    GraphInfoService graphInfoService;
   @Autowired
    FacilityInfoService facilityInfoService;
   @Autowired
    FeeValueService feeValueService;
   @Autowired
   ApartmentFacilityService apartmentFacilityService;

   @Autowired
   ApartmentLabelService apartmentLabelService;
   @Autowired
   ApartmentFeeValueService apartmentFeeValueService;
   @Autowired
   ProvinceInfoService provinceInfoService;
   @Autowired
   CityInfoService cityInfoService;
   @Autowired
   DistrictInfoService districtInfoService;

   @Autowired
   RoomInfoService roomInfoService;
    @Override
    public IPage<ApartmentItemVo> getPageApartmentItemVoByQueryVo(Page<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.getPageApartmentItemVoByQueryVo(page,queryVo);
    }

    @Override
    public ApartmentDetailVo getApartmentDetailVoById(Long id) {
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
        List<GraphVo> graphVoList=graphInfoService.getGraphvoListByApartmentId(id);
          List<FacilityInfo> facilityInfoList=facilityInfoService.getApartmentFacilityById(id);
          List<FeeValueVo> feeValueVoList=feeValueService.getFeeValueListByApartmenId(id);
          List<LabelInfo> labelInfoList=labelInfoService.getLabelsByApartmentId(id);
          apartmentDetailVo.setGraphVoList(graphVoList);
          apartmentDetailVo.setFacilityInfoList(facilityInfoList);
          apartmentDetailVo.setFeeValueVoList(feeValueVoList);
          apartmentDetailVo.setLabelInfoList(labelInfoList);
        return  apartmentDetailVo;
    }

    @Override
    public void saveOrUpdateApartmentVo(ApartmentSubmitVo apartmentSubmitVo) {
        Long id = apartmentSubmitVo.getId();
        apartmentSubmitVo.setProvinceName(provinceInfoService.getById(apartmentSubmitVo.getProvinceId()).getName());
        apartmentSubmitVo.setCityName(cityInfoService.getById(apartmentSubmitVo.getCityId()).getName());
        apartmentSubmitVo.setDistrictName(districtInfoService.getById(apartmentSubmitVo.getDistrictId()).getName());
        //判断是增加还是修改
        if(id!=null)
        {
            //是修改就先把公寓的中间表信息全部删除
            apartmentFacilityService.remove(new LambdaQueryWrapper<ApartmentFacility>().eq(ApartmentFacility::getApartmentId,id));
            apartmentLabelService.remove(new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId,id));
            apartmentFeeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>().eq(ApartmentFeeValue::getApartmentId,id));
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemType,ItemType.APARTMENT).eq(GraphInfo::getItemId,id));
        }

        this.saveOrUpdate(apartmentSubmitVo);
        Long newId = apartmentSubmitVo.getId();
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        ArrayList<ApartmentFacility> apartmentFacilities = new ArrayList<>();
        facilityInfoIds.forEach(ids->{
            ApartmentFacility build = ApartmentFacility.builder().apartmentId(newId).facilityId(ids).build();
            apartmentFacilities.add(build);
        });
        apartmentFacilityService.saveBatch(apartmentFacilities);

        ArrayList<ApartmentLabel> apartmentLabels = new ArrayList<>();
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        labelIds.forEach(ids->{
            ApartmentLabel build = ApartmentLabel.builder().apartmentId(newId).labelId(ids).build();
            apartmentLabels.add(build);
        });
        apartmentLabelService.saveBatch(apartmentLabels);

        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        ArrayList<ApartmentFeeValue> apartmentFeeValues = new ArrayList<>();
        feeValueIds.forEach(ids->{
            ApartmentFeeValue build = ApartmentFeeValue.builder().apartmentId(newId).feeValueId(ids).build();
            apartmentFeeValues.add(build);
        });
        apartmentFeeValueService.saveBatch(apartmentFeeValues);

        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        ArrayList<GraphInfo> graphInfos = new ArrayList<>();
        graphVoList.forEach(ids->{
            GraphInfo graphInfo = new GraphInfo();
            graphInfo.setName(ids.getName());
            graphInfo.setUrl(ids.getUrl());
            graphInfo.setItemType(ItemType.APARTMENT);
            graphInfo.setItemId(newId);
            graphInfos.add(graphInfo);
        });
        graphInfoService.saveBatch(graphInfos);

    }

    @Override
    public void removeByApartmentId(Long id) {
        //如果公寓下有房间就不能删除

        long count = roomInfoService.count(new LambdaQueryWrapper<RoomInfo>().eq(RoomInfo::getApartmentId, id));
        if(count>0)throw new MyBusinessException(ResultCodeEnum.DELETE_ERROR);

        apartmentFacilityService.remove(new LambdaQueryWrapper<ApartmentFacility>().eq(ApartmentFacility::getApartmentId,id));
        apartmentLabelService.remove(new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId,id));
        apartmentFeeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>().eq(ApartmentFeeValue::getApartmentId,id));
        graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemType,ItemType.APARTMENT).eq(GraphInfo::getItemId,id));
        this.removeById(id);
    }
}




